<template>
  <div
    v-loading="$store.getters.loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="#a19f9fb0;"
  >
    <lb-layout>
      <aps-form
        :config="apsForm.formConfig"
        :model="apsForm.formData"
        :formRef="apsForm.formRef"
        :labelWidth="apsForm.labelWidth"
        ref="form"
      ></aps-form>
      <lb-table
        :column="tableData.column"
        :data="tableData.data"
        pagination
        background
        height="600"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[20, 40, 60, 80]"
        :pager-count="5"
        :current-page.sync="currentPage"
        :total="total"
        :page-size="pageSize"
        @size-change="handleSizeChange"
        @p-current-change="handleCurrentChange"
      >
      </lb-table>
    </lb-layout>
    <detail :visible.sync="show" :id="id" @close="close"></detail>
  </div>
</template>

<script>
import { list } from "@/api/iot/water";
import detail from "@/views/work/components/waterDetail.vue";
import RemoteSelect from "@/components/Select/RemoteSelect";
export default {
  name: "water",
  dicts: ["server_type", "device_model", "set_status", "set_type"],
  components: {
    detail,
    RemoteSelect,
  },
  data() {
    return {
      show: false,
      id: "",
      sysMonitoringNetworkId: "",
      apsForm: {
        formRef: "form",
        labelWidth: "auto",
        formData: {},
        formConfig: {
          formAttr: {
            inline: true,
            size: "small",
          },
          properties: {
            //这里是渲染查询表单的表头和类型的数据
            timeStamp: {
              label:"设置月份:",
              typeName: "el-date-picker",
              type: "month",
              placeholder: "请选择设置月份",
              "value-format": "timestamp",
            },
            setType: {
              label:"操作类型:",
              typeName: "el-select",
              render: (h) => {
                let that = this;
                let arr = [];
                that.dict?.type?.set_type.some((item) => {
                  arr.push(item);
                });
                // 数据严谨处理,处理后端数据异常情况
                let valNum = that.$refs?.form?.getFieldsValue().setType;

                let a = [
                  h(
                    "el-select",
                    {
                      attrs: {
                        placeholder: "请选择操作类型",
                        value: valNum,
                      },
                      props: ["value"],
                      on: {
                        change: (value) => {
                          that.$refs?.form?.setFieldValue("setType", value);
                          that.$forceUpdate();
                        },
                      },
                    },
                    [
                      arr.map((item) => {
                        let { label, value } = item;
                        return h("el-option", {
                          props: {
                            label,
                            value: parseInt(value),
                            key: value,
                          },
                        });
                      }),
                    ]
                  ),
                ];
                return h("div", a);
              },
            },
            sysMonitoringNetworkId: {
              label:"能耗终端:",
              typeName: "el-select",
              render: (h) => {
                return (
                  <div>
                    <RemoteSelect
                      v-model={this.sysMonitoringNetworkId}
                      varient="networks"
                      placeholder="请选择能耗终端"
                      size={"small"}
                    ></RemoteSelect>
                  </div>
                );
              },
            },
            deviceModel: {
              label:"智能设备:",
              typeName: "el-select",
              render: (h) => {
                let that = this;
                let arr = [];
                that.dict?.type?.device_model.some((item) => {
                  arr.push(item);
                });
                // 数据严谨处理,处理后端数据异常情况
                let valNum = that.$refs?.form?.getFieldsValue().deviceModel;

                let a = [
                  h(
                    "el-select",
                    {
                      attrs: {
                        placeholder: "请选择智能设备",
                        value: valNum,
                      },
                      props: ["value"],
                      on: {
                        change: (value) => {
                          that.$refs?.form?.setFieldValue("deviceModel", value);
                          that.$forceUpdate();
                        },
                      },
                    },
                    [
                      arr.map((item) => {
                        let { label, value } = item;
                        return h("el-option", {
                          props: {
                            label,
                            value: parseInt(value),
                            key: value,
                          },
                        });
                      }),
                    ]
                  ),
                ];
                return h("div", a);
              },
            },
          },
          formSoltButton: [
            {
              label: "搜索",
              type: "primary",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-search",
              click: this.handlerSearchDetail,
            },
            {
              label: "重置",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-refresh-left",
              click: this.handleReset,
            },
          ],
        },
      },
      tableData: {
        column: [
          {
            prop: "sysMonitoringNetworkName",
            label: "监测点名称",
            showOverflowTooltip: true,
            minWidth: 170,
          },
          {
            prop: "deviceSerialNum",
            label: "设备名称",
            minWidth: 170,
            showOverflowTooltip: true,
          },
          {
            prop: "setType",
            label: "操作类型",
            width: 150,
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.set_type}
                    value={scope.row.setType}
                  />
                </div>
              );
            },
          },
          {
            prop: "communicationMode",
            label: "通信方式",
            render: (h, scope) => {
              const { communicationMode } = scope.row;
              return <div>{communicationMode == 1 ? "4G" : ""}</div>;
            },
          },
          {
            prop: "setTime",
            label: "控制交互时间",
            width: 180,
          },
          {
            prop: "sysUserName",
            label: "操作人员",
          },
          {
            prop: "setStatus",
            label: "设置结果",
            minWidth: 150,
            showOverflowTooltip: true,
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.set_status}
                    value={scope.row.setStatus}
                  />
                </div>
              );
            },
          },
          {
            label: "操作",
            width: "150",
            fixed: "right",
            render: (h, scope) => {
              let that = this;
              return (
                <div>
                  {
                    <el-button
                      v-hasPermi={["device:operation:query"]}
                      type="text"
                      size="small"
                      icon="el-icon-view"
                      onClick={() => {
                        this.id = scope.row.id;
                        this.show = true;
                      }}
                    >
                      详情
                    </el-button>
                  }
                </div>
              );
            },
          },
        ],
        data: [],
      },
      currentPage: 1,
      pageSize: 20,
      total: 0,
    };
  },
  mounted() {
    this.initData();
  },
  methods: {
    close(status) {
      this.show = false;
      if (status) this.initData();
    },
    async initData(currentPage, pageSize, data) {
      let that = this;
      let query = {
        ...data,
      };
      if (currentPage) {
        query.pageNum = currentPage;
      } else {
        query.pageNum = that.currentPage;
      }
      if (pageSize) {
        query.pageSize = pageSize;
      } else {
        query.pageSize = that.pageSize;
      }
      this.$store.commit("app/updateApiCount", "add");
      await list(query)
        .then((res) => {
          this.$store.commit("app/updateApiCount");
          if (res.code === 700) {
            that.$message.warning(res.msg);
            return false;
          }
          that.tableData.data = res.rows;
          that.total = res.total;
        })
        .catch((err) => {
          this.$store.commit("app/updateApiCount");
          // this.$message.error("系统错误,请联系管理员!10102");
        });
    },
    handlerSearchDetail(val) {
      //val===1 删除操作成功后分页边界值处理
      let currentPage = 1;
      if (val === 1) {
        if (
          this.total === (this.currentPage - 1) * this.pageSize + 1 &&
          this.total !== 0 &&
          this.currentPage > 1
        ) {
          currentPage = this.currentPage - 1;
          this.currentPage = currentPage;
        } else {
          currentPage = this.currentPage;
        }
      } else {
        currentPage = this.currentPage;
      }
      const data = {
        ...this.$refs?.form?.getFieldsValue(),
        sysMonitoringNetworkId: this.sysMonitoringNetworkId,
      };
      data.timeStamp = data.timeStamp ? data.timeStamp / 1000 : null;
      this.initData(currentPage, this.pageSize, data);
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.handlerSearchDetail();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.handlerSearchDetail();
    },
    handleReset() {
      //重置
      this.sysMonitoringNetworkId = "";
      this.$refs?.form?.resetFields();
      this.handlerSearchDetail();
    },
  },
};
</script>

<style scoped lang="scss">
::v-deep .lb_layout {
  padding-top: 10px;
  padding-bottom: 10px;
}
</style>
